package demo.A;

import java.util.*;

/**
 * @ClassName : test
 * @Description :
 * @Author : JB
 * @Date: 2020-10-04 22:44
 */

 class Person{
    Integer age;
    String name;

    public Person(Integer age, String name) {
        this.age = age;
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

public class test {
    public static void main(String[] args) {
        Person p1 = new Person(2,"小明");
        Person p2 = new Person(1,"小宏");
        Person p3 = new Person(5,"小拉");
        Person p4 = new Person(3,"小中");
        List<Person> personList = new ArrayList<>();
        personList.add(p1);
        personList.add(p2);
        personList.add(p3);
        personList.add(p4);
        System.out.println(personList);

        Collections.sort(personList, new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.getAge().compareTo(o2.getAge());
               // return  o1.getName().compareTo(o2.getName());
            }
        });

        System.out.println(personList);

    }
}
